<?php
namespace Admin\Controller;
use Common\Controller\AdminController;

class LinkController extends AdminController{

	public function index(){
		$where = '1=1';
		$where .= I('get.cid') > 0 ? ' and cid=' . I('get.cid', 0, 'intval') : '';
		$where .= I('get.name') ? ' and name like "%' . I('get.name') . '%"' : '';
		$where .= I('get.status_query', -1) > -1 ? ' and status =' . I('get.status_query') : '';
		$list = $this->getList('id,name,pic,link,sort,status', 'link', $where, 'sort desc,id desc');
		$this->assign('list', $list);

		$classifyList = $this->getList('id,name', 'link_classify', '1=1', 'sort desc');
		$this->assign('classifyList', $classifyList);
		$this->display();
	}

	public function add(){
		if(IS_POST){
			if(!$_POST['cid']){
				$this->returnResult(false,"请维护分类");
			}
			if(!$_POST['pic']){
				$this->returnResult(false,"上传图片");
			}
			$_POST['sort'] = $_POST['set_sort'];
			$_POST['status'] = $_POST['set_status'];
			unset($_POST['set_sort']);
			unset($_POST['set_status']);

			$result = $this->updateData($_POST, 'link');
			$this->returnResult($result);
		}else{
			$list = $this->getList('id,name', 'link_classify', null, 'sort desc,id asc');
			$this->assign('list', $list);
			$this->display();
		}
	}

	public function edit(){
		if(IS_POST){
			$_POST['sort'] = $_POST['set_sort'];
			$_POST['status'] = $_POST['set_status'];
			unset($_POST['set_sort']);
			unset($_POST['set_status']);
			$result = $this->updateData($_POST, 'link',2);
			$this->returnResult($result);
		}else{
			$info = $this->getInfo('*', 'link', array('id' => I('get.id', 0, 'intval')));
			if($info){
				$data['info'] = $info;
				$data['status'] = 1;
			}else{
				$data['info'] = '数据不存在';
				$data['status'] = 0;
			}
			$this->ajaxReturn($data);
		}
	}

	public function del(){
		$result = $this->deleteData('id=' . I('get.id', 0, 'intval'), 'link');
		$this->returnResult($result);
	}

	public function classify(){
		$list = $this->getList('*', 'link_classify', null, 'sort desc');
		$this->assign('list', $list);
		$this->display();
	}

	public function addClassify(){
		if(IS_POST){
			$result = $this->updateData($_POST, 'link_classify');
			$this->returnResult($result, null, U('Link/classify'));
		}
	}

	public function editClassify(){
		if(IS_POST){
			$result = $this->updateData($_POST, 'link_classify', 2);
			$this->returnResult($result, null, U('Link/classify'));
		}else{
			$info = $this->getInfo('*', 'link_classify', array('id' => I('get.id', 0, 'intval')));
			$this->ajaxReturn($info);
		}
	}

	public function delClassify(){
		$result = $this->deleteData('id=' . I('get.id', 0, 'intval'), 'link_classify');
		$this->returnResult($result);
	}
}